package cn.supermarket.service.impl;

import cn.supermarket.pojo.dto.ProductInsertDTO;
import cn.supermarket.pojo.dto.ProductUpdateDTO;
import cn.supermarket.pojo.entity.Product;
import cn.supermarket.ex.ServiceException;
import cn.supermarket.mapper.ProductMapper;
import cn.supermarket.service.iservice.IProductService;
import cn.supermarket.utils.SnowFlakeGenerateIdWorker;
import cn.supermarket.web.JsonPage;
import cn.supermarket.web.State;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;


@Service
public class ProductServiceImpl implements IProductService{



    @Autowired
    private ProductMapper mapper;


    /**
     * 添加商品信息
     * @param productInsertDTO
     * @return
     */
    @Override
    public int addNew(ProductInsertDTO productInsertDTO){


        Product product = new Product();
        BeanUtils.copyProperties(productInsertDTO,product);
        SnowFlakeGenerateIdWorker get = new SnowFlakeGenerateIdWorker(7,17);
        long id = get.nextId();
        product.setId(id);
        product.setProductId(productInsertDTO.getProductId());
        product.setIsOffer(0);
        product.setRebates(0);

        int rows = mapper.insert(product);

        if (rows != 1){
            throw new ServiceException(State.ERR_PRODUCT_INSERT,"商品信息写入错误,请稍后再次尝试");
        }
        return rows;
    }




    /**
     * 删除商品
     * @param ids
     */
    @Override
    public void deleteProduct(Long[] ids) {
        int rows = mapper.deleteProduct(ids);
        if (rows==0){
            throw new ServiceException(State.ERR_NOT_FOUND,"没有查询到需要删除的商品,删除商品失败");
        }
    }

    /**
     * 修改商品信息
     */
    @Override
    public void updateProductById(Long id, ProductUpdateDTO productUpdateDTO) {
        Product queryResult = mapper.selectById(id);
        if (queryResult==null){
            throw new ServiceException(State.ERR_NOT_FOUND,"修改失败,查询的数据目前不存在");
        }
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateDTO,product);
        product.setId(id);
        product.setLastUpdateTime(LocalDateTime.now());
        int rows = mapper.updateProductById(product);
        if (rows==0){
            throw new ServiceException(State.ERR_NOT_FOUND,"修改信息失败,服务器忙,请稍后再次尝试");
        }
    }



    @Override
    public Product selectById(Long id) {
        Product product = mapper.selectById(id);
        if(product == null){
            throw new ServiceException(State.ERR_PRODUCT_SELECT_NULL,"商品信息查询为空,请稍后再次尝试");
        }
        return product;
    }

    @Override
    public Product selectByProductId(Integer productId) {
        Product product = mapper.selectByProductId(productId);
        if(product == null){
            throw new ServiceException(State.ERR_PRODUCT_SELECT_NULL,"商品信息查询为空,请稍后再次尝试");
        }
        return product;
    }

    @Override
    public JsonPage<Product> selectByTitle(Integer pageNum, Integer pageSize,String title) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> list = mapper.selectByTitle(title);
        if(list == null){
            throw new ServiceException(State.ERR_PRODUCT_SELECT_NULL,"商品信息查询为空,请稍后再次尝试");
        }
        return JsonPage.restPage(new PageInfo<>(list));
    }


}
